package com.exam.api.controller;

import com.exam.api.common.BaseController;
import com.exam.api.common.ReturnMsg;
import com.exam.api.dto.ExamDTO;
import com.exam.api.service.ExamService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.exam.api.common.BaseSelectEntity;
import java.util.ArrayList;
import com.exam.api.service.TeacherService;
import com.exam.api.dto.TeacherDTO;
import javax.annotation.Resource;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
import com.exam.api.common.BaseSelectEntity;
import cn.hutool.core.collection.CollectionUtil;
import java.util.Date;
/**
* @description: 考试信息对外接口层
* @author: wuming
* @create: 2024.05.03 13:24:03
*/
@RestController
@RequestMapping("/exam")
public class ExamController extends BaseController<ExamService, ExamDTO> {

    @Autowired
    private TeacherService teacherService;

    @Override
    @Resource(name = "examService")
    protected void setBaseService(ExamService examService) {
        this.baseService = examService;
    }

    /**
    * 默认分页查询
    * @param examDTO
    * @return
    */
    @PostMapping("/page")
    public ReturnMsg page(@RequestBody ExamDTO examDTO) {
        return ReturnMsg.ok(baseService.basePage(examDTO));
    }

    /**
    * 根据XML中的sql语句进行查询
    * @param examDTO
    * @return
    */
    @PostMapping(value = "/findList")
    public ReturnMsg findList(@RequestBody ExamDTO examDTO) {
        return ReturnMsg.ok(baseService.findList(examDTO));
    }

    @Override
    protected void beforeSave(ExamDTO entity) throws Exception {
        entity.setCreateTime(new Date());
        super.beforeSave(entity);
    }

    @GetMapping("/all/teacher")
    public ReturnMsg allTeacher() {
        List<BaseSelectEntity> list = new ArrayList<>();
        List<TeacherDTO> dtos = teacherService.list();
            for (TeacherDTO dto : dtos) {
            BaseSelectEntity baseSelectEntity = new BaseSelectEntity();
            baseSelectEntity.setKey(dto.getId());
            baseSelectEntity.setValue(dto.getName());
            list.add(baseSelectEntity);
        }
        return ReturnMsg.ok(list);
    }

}

